package goods

import (
	"context"
	"encoding/json"
	"fmt"
	redis2 "gitee.com/xuxinzhi2206/corehub/database/redis"
	goods2 "goods-rpc/internal/server/goods"
	"goods-rpc/models"
	good "goods-rpc/proto/goods"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"time"
)

type GoodsService struct {
	good.UnimplementedGoodServer
}

func (l *GoodsService) GoodsRelease(ctx context.Context, req *good.GoodsReleaseReq) (*good.GoodsReleaseResp, error) {
	if req.GoodsName == "" {
		return nil, status.Error(codes.Unimplemented, "用户名不能为空")
	}
	if req.GoodsNum == 0 {
		return nil, status.Error(codes.Unimplemented, "购买数量不能为空")

	}
	if req.GoodsStats == "" {
		return nil, status.Error(codes.Unimplemented, "商品属性不能为空")

	}
	if req.GoodsPrice == 0 {
		return nil, status.Error(codes.Unimplemented, "商品售价不能为空")

	}
	if req.GoodsShop == "" {
		return nil, status.Error(codes.Unimplemented, "商品店铺不能为空")

	}
	if req.GoodsUrl == "" {
		return nil, status.Error(codes.Unimplemented, "商品图片不能为空")

	}
	if req.CountPrice == 0 {
		return nil, status.Error(codes.Unimplemented, "总价格不能为空")
	}

	goods := models.Goods{
		GoodsName:   req.GoodsName,
		GoodsStats:  req.GoodsStats,
		GoodsUrl:    req.GoodsUrl,
		GoodsShop:   req.GoodsShop,
		GoodsNum:    int(req.GoodsNum),
		GoodsPrice:  float64(req.GoodsPrice),
		CouponPrice: float64(req.CouponPrice),
		CountPrice:  float64(req.CountPrice),
		Preheating:  int(req.Preheating),
		GoodsStatus: int(req.GoodsStatus),
		GoodsSort:   int(req.GoodsSort),
	}
	takeAdd, err := goods2.TakeAdd(goods)
	if err != nil {
		return nil, err
	}
	return &good.GoodsReleaseResp{
		ReleaseInfo: takeAdd,
	}, nil
}

func (l *GoodsService) GoodsDel(ctx context.Context, req *good.GoodsDelReq) (*good.GoodsDelResp, error) {
	if req.Id == 0 {
		return nil, status.Error(codes.Unimplemented, "id不能为空")
	}
	_, err := goods2.TakeDel(int(req.Id))
	if err != nil {
		return nil, err
	}
	return &good.GoodsDelResp{}, nil
}

func (l *GoodsService) GoodsList(ctx context.Context, req *good.GoodsListReq) (*good.GoodsListResp, error) {
	takeList, err := goods2.TakeList()
	if err != nil {
		return nil, err
	}
	return &good.GoodsListResp{List: takeList}, nil
}

func (l *GoodsService) GoodsSearchByID(ctx context.Context, req *good.GoodsSearchReq) (*good.GoodsSearchResp, error) {
	if req.Id == 0 {
		return nil, status.Error(codes.Unimplemented, "id不能为空")
	}
	takeFind, err := goods2.TakeFind(int(req.Id))
	if err != nil {
		return nil, err
	}
	marshal, err := json.Marshal(takeFind)
	if err != nil {
		return nil, err
	}
	err = redis2.SetRedis("SearchInfo", string(marshal), time.Hour*5)
	if err != nil {
		return nil, err
	}
	return &good.GoodsSearchResp{SearchINfo: takeFind}, nil
}

func (l *GoodsService) GoodsFindBySort(ctx context.Context, req *good.GoodsFindBySortReq) (*good.GoodsFindBySortResp, error) {
	if req.GoodsSort == 0 {
		return nil, status.Error(codes.Unimplemented, "id不能为空")
	}
	takeFindSort, err := goods2.TakeFindSort(int(req.GoodsSort))
	if err != nil {
		return nil, err
	}
	return &good.GoodsFindBySortResp{SearchSort: takeFindSort}, nil
}

func (l *GoodsService) GoodsPreheating(ctx context.Context, req *good.GoodsPreheatingReq) (*good.GoodsPreheatingResp, error) {
	if req.Preheating != 1 && req.Preheating != 2 {
		return nil, status.Error(codes.Unimplemented, "Preheating不能为空")
	}
	preheating, err := goods2.TakePreheating()
	if err != nil {
		return nil, err
	}
	return &good.GoodsPreheatingResp{FindPreheating: preheating}, nil
}

func (l *GoodsService) GoodsUpdateByNUm(ctx context.Context, req *good.GoodsUpdateByNUmReq) (*good.GoodsUpdateByNUmResp, error) {
	if req.ID == 0 {
		return nil, status.Error(codes.Unimplemented, "id不能为空")
	}
	if req.GoodsNUm == 0 {
		return nil, status.Error(codes.Unimplemented, "GoodsNUm不能为0")
	}
	updateBYNum, err := goods2.TakeUpdateBYNum(int(req.ID), int(req.GoodsNUm))
	if err != nil {
		return nil, err
	}
	fmt.Println(updateBYNum)
	return &good.GoodsUpdateByNUmResp{GoodsNUm: 377}, nil
}
